﻿using System;

namespace ZOIL.DataModel
{
    public class DisposableCreator : IDisposable
    {
        private readonly Action _releaseResources;

        #region ctor
        public DisposableCreator(Action allocateResources, Action releaseResources)
        {
            _releaseResources = releaseResources;

            allocateResources();
        }

        #endregion


        #region Implementation of IDisposable

        private bool _disposed = false;
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            if(!_disposed)
            {
                _releaseResources();
                _disposed = true;
            }
        }

        #endregion
    }
}